﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Common;
using Taobao.Top.Api;
using System.Text;
using System.Security.Cryptography;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;
using System.Data;

public partial class top_updateUserInfo : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string u = utils.NewRequest("u", utils.RequestType.QueryString);
        string t = utils.NewRequest("t", utils.RequestType.QueryString);

        if (u == "")
            return;

        string sql = "SELECT TOP 100 * FROM TopBlogAuto a INNER JOIN TopTaobaoShop s ON s.nick = a.nick AND s.isoverblog = 0 WHERE isauto = 1 AND keywords <> '' AND uids <> ''";
        DataTable dt = utils.ExecuteDataTable(sql);
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            CheckUser("0", dt.Rows[i]["nick"].ToString());
        }
    }

    private void CheckUser(string t, string u)
    {
        string appkey = "12132145";
        string secret = "1fdd2aadd5e2ac2909db2967cbb71e7f";

        IDictionary<string, string> param = new Dictionary<string, string>();
        param.Add("nick", u);
        if (t == "0")
        {
            appkey = "12159997";
            secret = "614e40bfdb96e9063031d1a9e56fbed5";
            param.Add("lease_id", "161220");
        }
        else
        {
            param.Add("lease_id", "133368");
        }
        string result = Post("http://gw.api.taobao.com/router/rest", appkey, secret, "taobao.appstore.subscribe.get", "", param);

        string enddate = string.Empty;
        string version = string.Empty;
        string sql = string.Empty;

        //Response.Write(result);

        if (result.IndexOf("invali") != -1 || result.IndexOf("<appstore_subscribe_get_response></appstore_subscribe_get_response>") != -1)
        {
            //到期了
            if (t == "0")
            {
                sql = "UPDATE TopTaobaoShop SET isoverblog = 1,enddateBlog = GETDATE() WHERE nick = '" + u + "'";
            }
            else
            {
                sql = "UPDATE TopTaobaoShop SET isover = 1,enddate = GETDATE() WHERE nick = '" + u + "'";
            }
            utils.ExecuteNonQuery(sql);
        }
        else
        {
            //更新日期
            enddate = new Regex(@"<end_date>([^<]*)</end_date>", RegexOptions.IgnoreCase).Match(result).Groups[1].ToString();
            version = new Regex(@"<version_no>([^<]*)</version_no>", RegexOptions.IgnoreCase).Match(result).Groups[1].ToString();

            if (t == "0")
            {
                sql = "UPDATE TopTaobaoShop SET enddateBlog = '" + enddate + "',versionNoBlog = '" + version + "' WHERE nick = '" + u + "'";
            }
            else
            {
                sql = "UPDATE TopTaobaoShop SET enddate = '" + enddate + "',versionNo = '" + version + "' WHERE nick = '" + u + "'";
            }
            utils.ExecuteNonQuery(sql);
        }
        Response.Write(sql + "<br><br>");
    }

    /// <summary> 
    /// 给TOP请求签名 API v2.0 
    /// </summary> 
    /// <param name="parameters">所有字符型的TOP请求参数</param> 
    /// <param name="secret">签名密钥</param> 
    /// <returns>签名</returns> 
    protected static string CreateSign(IDictionary<string, string> parameters, string secret)
    {
        parameters.Remove("sign");
        IDictionary<string, string> sortedParams = new SortedDictionary<string, string>(parameters);
        IEnumerator<KeyValuePair<string, string>> dem = sortedParams.GetEnumerator();
        StringBuilder query = new StringBuilder(secret);
        while (dem.MoveNext())
        {
            string key = dem.Current.Key;
            string value = dem.Current.Value;
            if (!string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(value))
            {
                query.Append(key).Append(value);
            }
        }
        query.Append(secret);
        MD5 md5 = MD5.Create();
        byte[] bytes = md5.ComputeHash(Encoding.UTF8.GetBytes(query.ToString()));
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < bytes.Length; i++)
        {
            string hex = bytes[i].ToString("X");
            if (hex.Length == 1)
            {
                result.Append("0");
            }
            result.Append(hex);
        }
        return result.ToString();
    }
    /// <summary> 
    /// 组装普通文本请求参数。 
    /// </summary> 
    /// <param name="parameters">Key-Value形式请求参数字典</param> 
    /// <returns>URL编码后的请求数据</returns> 
    protected static string PostData(IDictionary<string, string> parameters)
    {
        StringBuilder postData = new StringBuilder();
        bool hasParam = false;
        IEnumerator<KeyValuePair<string, string>> dem = parameters.GetEnumerator();
        while (dem.MoveNext())
        {
            string name = dem.Current.Key;
            string value = dem.Current.Value;
            // 忽略参数名或参数值为空的参数 
            if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(value))
            {
                if (hasParam)
                {
                    postData.Append("&");
                }
                postData.Append(name);
                postData.Append("=");
                postData.Append(Uri.EscapeDataString(value));
                hasParam = true;
            }
        }
        return postData.ToString();
    }
    /// <summary> 
    /// TOP API POST 请求 
    /// </summary> 
    /// <param name="url">请求容器URL</param> 
    /// <param name="appkey">AppKey</param> 
    /// <param name="appSecret">AppSecret</param> 
    /// <param name="method">API接口方法名</param> 
    /// <param name="session">调用私有的sessionkey</param> 
    /// <param name="param">请求参数</param> 
    /// <returns>返回字符串</returns> 
    public static string Post(string url, string appkey, string appSecret, string method, string session,
    IDictionary<string, string> param)
    {
        #region -----API系统参数----
        param.Add("app_key", appkey);
        param.Add("method", method);
        param.Add("session", session);
        param.Add("timestamp", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
        param.Add("format", "xml");
        param.Add("v", "2.0");
        param.Add("sign_method", "md5");
        param.Add("sign", CreateSign(param, appSecret));
        #endregion
        string result = string.Empty;
        #region ---- 完成 HTTP POST 请求----
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
        req.Method = "POST";
        req.KeepAlive = true;
        req.Timeout = 300000;
        req.ContentType = "application/x-www-form-urlencoded;charset=utf-8";
        byte[] postData = Encoding.UTF8.GetBytes(PostData(param));
        Stream reqStream = req.GetRequestStream();
        reqStream.Write(postData, 0, postData.Length);
        reqStream.Close();
        HttpWebResponse rsp = (HttpWebResponse)req.GetResponse();
        Encoding encoding = Encoding.GetEncoding(rsp.CharacterSet);
        Stream stream = null;
        StreamReader reader = null;
        stream = rsp.GetResponseStream();
        reader = new StreamReader(stream, encoding);
        result = reader.ReadToEnd();
        if (reader != null) reader.Close();
        if (stream != null) stream.Close();
        if (rsp != null) rsp.Close();
        #endregion
        return Regex.Replace(result, @"[\x00-\x08\x0b-\x0c\x0e-\x1f]", "");
    }
}